package xyz.riceball.usercore.controller;


import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.usercore.entity.dto.RouteDTO;
import xyz.riceball.usercore.entity.po.RoutePO;
import xyz.riceball.usercore.service.RouteService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 路由表(Route)表控制层
 *
 * @author xiaovcloud
 * @since 2022-04-24 20:17:49
 */
@Api(tags = "子功能管理")
@RestController
@RequestMapping("/rb/api/usercore/route")
@RbSysRequest
public class RouteController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private RouteService routeService;

    /**
     * 分页查询所有数据
     *
     * @param pageParam    分页对象
     * @param routePO 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询子功能")
    @GetMapping
    @RbLog(description = "分页查询子功能")
    public ApiR<Page<RoutePO>> selectAll(PageParam pageParam, RoutePO routePO) {
        return success(routeService.selectPage(PageParam.<RoutePO>of(pageParam), routePO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询子功能")
    @GetMapping("{id}")
    @RbLog(description = "根据id查询子功能")
    public ApiR<RoutePO> selectOne(@PathVariable Serializable id) {
        return success(routeService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param routeDTO 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增子功能")
    @PostMapping
    @RbLog(description = "新增子功能")
    public ApiR<RouteDTO> insert(@RequestBody RouteDTO routeDTO) {
        return routeService.add(routeDTO);
    }

    /**
     * 修改数据
     *
     * @param routeDTO 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改子功能")
    @PutMapping
    @RbLog(description = "修改子功能")
    public ApiR<Void> update(@RequestBody RouteDTO routeDTO) {
       boolean edit = routeService.edit(routeDTO);
        return ApiR.msg(edit,"修改子功能成功","修改子功能失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除子功能")
    @DeleteMapping
    @RbLog(description = "删除子功能")
    public ApiR<Void> delete(@RequestBody List<String> ids) {
        return routeService.delete(ids);
    }

//    /**
//     * 刷新所有路由数据
//     * 主要逻辑：已经存在的不变动，新增的新增，不在的删除
//     *
//     * @return
//     */
//    @ApiOperation(value = "刷新子功能（暂时不用）")
//    @PostMapping("/refresh")
//    public ApiR<Void> refresh() {
//        boolean refresh = routeService.refresh();
//        return ApiR.msg(refresh, "刷新成功", "刷新失败");
//    }

    @ApiOperation(value = "树形查询所有子功能")
    @GetMapping("/tree")
    @RbLog(description = "树形查询所有子功能")
    public ApiR<List<Tree<String>>> tree() {
        return success(routeService.tree());
    }
}

